/*
 * @Description: 生成Service
 * @Author: DHL
 * @Date: 2022-01-30 14:20:12
 * @LastEditors: DHL
 * @LastEditTime: 2022-07-20 13:12:13
 */

import { firstToUpperCase, firstToLowerCase } from '../../utils/codeGenUtils'

export function createService(codeGenConfig: any) {
  const classNameFirstToUpperCase = firstToUpperCase(codeGenConfig.className)
  const classNameFirstToLowerCase = firstToLowerCase(codeGenConfig.className)
  const variableMapper = classNameFirstToLowerCase + 'Mapper'
  const classNameInterface = 'I' + codeGenConfig.className + 'Service'
  const className = codeGenConfig.className + 'Service'

  return `
package ${codeGenConfig.packageName}.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import ${codeGenConfig.packageName}.entity.${classNameFirstToLowerCase}.${classNameFirstToUpperCase};
import ${codeGenConfig.packageName}.entity.${classNameFirstToLowerCase}.dto.Search${classNameFirstToUpperCase}Dto;
import ${codeGenConfig.packageName}.entity.${classNameFirstToLowerCase}.vo.Save${classNameFirstToUpperCase}Vo;
import ${codeGenConfig.packageName}.entity.${classNameFirstToLowerCase}.vo.Search${classNameFirstToUpperCase}Vo;
import ${codeGenConfig.packageName}.entity.${classNameFirstToLowerCase}.vo.Update${classNameFirstToUpperCase}ByIdVo;
import ${codeGenConfig.packageName}.mapper.${classNameFirstToUpperCase}Mapper;
import ${codeGenConfig.packageName}.service.I${classNameFirstToUpperCase}Service;
import com.twb.core.base.entity.TwbPageInfo;
import com.twb.core.base.service.impl.BaseService;
import com.twb.core.exception.ServiceException;
import com.twb.core.util.BeanUtils;
import com.twb.core.util.ExcelUtils;
import com.twb.core.util.TwbPageUtil;
import com.twb.system.util.ExcelDataDicHandlerImpl;

/**
 * @ClassName: ${className}
 * @Description: ${codeGenConfig.genName}Service
 * 
 * @author ${codeGenConfig.genAuthor}
 * @date ${codeGenConfig.createData}
 */
@Service("${classNameFirstToLowerCase}Service")
public class ${className} extends BaseService<${classNameFirstToUpperCase}Mapper, ${classNameFirstToUpperCase}> implements ${classNameInterface} {
  
  @Autowired
  ${classNameFirstToUpperCase}Mapper ${variableMapper};

  @Override
  public TwbPageInfo<Search${classNameFirstToUpperCase}Dto> search${classNameFirstToUpperCase}(Search${classNameFirstToUpperCase}Vo vo) {
    TwbPageUtil.startPage(vo);
    List<Search${classNameFirstToUpperCase}Dto> list = ${variableMapper}.search${classNameFirstToUpperCase}(vo);
    return TwbPageUtil.of(list);
  }

  @Override
  public Search${classNameFirstToUpperCase}Dto search${classNameFirstToUpperCase}Sum(Search${classNameFirstToUpperCase}Vo vo) {
    Search${classNameFirstToUpperCase}Dto result = ${variableMapper}.search${classNameFirstToUpperCase}Sum(vo);
    return result;
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public boolean save${classNameFirstToUpperCase}(Save${classNameFirstToUpperCase}Vo vo) {
    ${classNameFirstToUpperCase} checkVo = new ${classNameFirstToUpperCase}();
    // 此处需要修改为需要校验重复的字段
    checkVo.setName(vo.getName());
    checkVo.setIsdel(0);
    long count = this.count(checkVo);
    if (count > 0) {
      throw new ServiceException(String.format("[%s]已存在", checkVo.getName()));
    }

    ${classNameFirstToUpperCase} record = new ${classNameFirstToUpperCase}();
    BeanUtils.copyProperties(vo, record);
    record.setId(this.getUUID());
    this.setCreateInfo(record);
    this.setUpdateInfo(record);
    return this.save(record);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public boolean update${classNameFirstToUpperCase}ById(Update${classNameFirstToUpperCase}ByIdVo vo) {
    ${classNameFirstToUpperCase} record = ${variableMapper}.selectById(vo.getId());
    if (null == record) {
      throw new ServiceException("数据不存在");
    }

    if (!vo.getName().equalsIgnoreCase(record.getName())) {
      ${classNameFirstToUpperCase} checkVo = new ${classNameFirstToUpperCase}();
      // 此处需要修改为需要校验重复的字段
      checkVo.setName(vo.getName());
      checkVo.setIsdel(0);
      long count = this.count(checkVo);
      if (count > 0) {
        throw new ServiceException(String.format("[%s]已存在", checkVo.getName()));
      }
    }

    BeanUtils.copyProperties(vo, record);
    this.setUpdateInfo(record);
    return this.updateById(record);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public boolean delete${classNameFirstToUpperCase}ByIds(String ids) {
    return this.removeByIds(Arrays.asList(ids.split(",")));
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public int logicBatchDelete${classNameFirstToUpperCase}ByIds(String ids) {
    List<String> list = new ArrayList<String>();
    Collections.addAll(list, ids.split(","));
    return ${variableMapper}.logicBatchDelete${classNameFirstToUpperCase}ByIds(list);
  }

  @Override
  public void exportExcel(Search${classNameFirstToUpperCase}Vo vo, HttpServletResponse response) throws Exception {
    String fileName = vo.getFileName();
    if(StringUtils.isBlank(fileName)) {
      fileName="${codeGenConfig.genName}";
    }
    TwbPageInfo<Search${classNameFirstToUpperCase}Dto> pageInfo = this.search${classNameFirstToUpperCase}(vo);
    List<Search${classNameFirstToUpperCase}Dto> list = pageInfo.getList();
    ExcelDataDicHandlerImpl excelDataDicHandlerImpl = new ExcelDataDicHandlerImpl();
    ExcelUtils.exportExcel(fileName, list, Search${classNameFirstToUpperCase}Dto.class, response,excelDataDicHandlerImpl);
  }
}
`
}
